/**
**
**    修改apk属性的工具类
**      notice 若需要反编译后修改apk则需要apktool支持
**      author:zhouzhineng
** 
**/

var shell   = require('shelljs'),
    exec    = require('./exec'),
    spawn   = require('./spawn'),
    Q       = require('q'),
    path    = require('path'),
    fs      = require('fs');


    var manifeast,strings,appicon;
    var g_mainfeast,g_strings;
    /**
  **  初始化
  **@param projectPath chamleon-v2的路径
    **/
    module.exports.init = function(projectPath) {
      manifeast = path.join(projectPath,"AndroidManifest.xml");
      strings = path.join(projectPath,"res","values","strings.xml");
      appicon = path.join(projectPath,"res","drawable","appicon.png");
    }


    module.exports.uninit = function() {
      manifeast = null;
      strings = null;
      appicon = null;
    }
    
    //反编译apk
    /**
    ** @param apkFile 需要被反编译的apk
    ** @param unpackDir 反编译出来的apk目录输出路径
    **/
   module.exports.unpacketApk = function(apkFile,unpackDir) {
     var cmd = "apktool d -f " + apkFile + " -o " + unpackDir;
     exec(cmd, {maxBuffer: 10*1024*1024}, function(err, stdout, stderr) {
        if(err) {
          console.error(err);
        }
      });
   }

   //重新打包反编译后的apk
   /**
   **@param unpackDir 反编译后出来的apk目录
   **/
   module.exports.packetApk = function(unpackDir) {
     var cmd = "apktool b " + unpackDir;
      exec(cmd, {maxBuffer: 10*1024*1024}, function(err, stdout, stderr) {
        if(err) {
          console.error(err);
        }
      });
   }


   //加载AndroidManifeast.xml到内存
   function loadAndroidManifeast() {
      if(g_mainfeast == null) {
        g_mainfeast = fs.readFileSync(manifeast,'utf-8');
      }
   }

   //保存配置到AndroidManifeast.xml
   function storeAndroidManifeast() {
      if(g_mainfeast != null) {
        fs.writeFileSync(manifeast,g_mainfeast,'utf-8');
      }
      g_mainfeast = null;
   }

   //加载strings.xml到内存
   function loadStrings() {
      if(g_strings == null) {
        g_strings = fs.readFileSync(strings,'utf-8');
      }
   }

   //保存配置到strings.xml
   function storeStrings() {
      if(g_strings != null) {
        fs.writeFileSync(strings,g_strings,'utf-8');
      }
      g_strings = null;
   }

   //just for chameleon-v2 AndroidManifeast.xml
   function replaceContent(content,oldProperty,newProperty) {
      if(content != null && oldProperty != null) {
        var startIdx,endIdx;
        var subContent;
        startIdx = content.indexOf(oldProperty);
        if(startIdx != -1) {
          subContent = content.substring(startIdx,content.length);
          endIdx = subContent.indexOf("\n");
          subContent = subContent.substring(0,endIdx);
          content = content.replace(subContent,newProperty);
        }
      }
      return content;
   }

   //设置版本信息
   module.exports.setVersionInfo =function(versionCode,versionName) {
      
      loadAndroidManifeast();
      
      var startIdx,endIdx;
      var subXml;
      if(versionCode != null) {
        g_mainfeast = replaceContent(g_mainfeast,'android:versionCode','android:versionCode=' + '"' + versionCode + '"');
      }

      if(versionName != null) {
        g_mainfeast = replaceContent(g_mainfeast,'android:versionName','android:versionName=' + '"' + versionName + '"');
      }

      storeAndroidManifeast();

   }

   module.exports.setSdkInfo = function(minSdk,targetSdk) {
      loadAndroidManifeast();
      var startIdx,endIdx;
      var subXml;
      if(minSdk != null) {
        g_mainfeast = replaceContent(g_mainfeast,'android:minSdkVersion','android:minSdkVersion=' + '"' + minSdk + '"');
      }

      if(targetSdk != null) {
        g_mainfeast = replaceContent(g_mainfeast,'android:targetSdkVersion','android:targetSdkVersion=' + '"' + targetSdk + '"');
      }

      console.log(g_mainfeast);
      storeAndroidManifeast();
   }

   //just for chameleon-v2 strings.xml
   /**
   ** @param appName 应用名
   ** @param appIcon 应用图标
   **/
   module.exports.setAppInfo = function(appName,appIcon) {
      if(appName != null) {
        loadStrings();
        var strings = g_strings.split("\n");
        var appnameLine = null;
        for(var i = 0; i < strings.length; i++) {
          if(strings[i].indexOf("app_name") != -1) {
            appnameLine = strings[i];
            break;
          }
        }
        if(appnameLine != null) {
          var tmp , oldAppName ,appname;
          tmp = appnameLine.substring(appnameLine.indexOf(">"),appnameLine.length);
          oldAppName = tmp.substring(1,tmp.indexOf("<"));
          appname = appnameLine.replace(oldAppName,appName);
          g_strings = g_strings.replace(appnameLine,appname);
          storeStrings();
        }
      }
      if(appIcon != null) {
        if(fs.existsSync(appicon)) {
          shell.rm('-rf', appicon);
        }
        shell.mv(appIcon,appicon);
      }
   }

//loadAndroidManifeast();
//设置版本信息
//setVersionInfo(20,'20.0.0');
//loadStrings();
//setAppInfo("色龙",null);
//setSdkInfo('12','19');





